﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TimeCapsule;

namespace TimeCapsule
{
	public partial class Register : System.Web.UI.Page
	{
		public static Member mb;
		
		protected void Page_Load(object sender, EventArgs e)
		{
			mb = new Member();
			
			if( mb.key == "" )
			{
				lblTitle.Text = "회원 가입";
				ClientScript.RegisterHiddenField("hdnValues", "");
				btnSave.Visible = true;
				btnModify.Visible = false;
			}
			else
			{
				lblTitle.Text = "정보 변경";
				string str = "";
				string param = "~//~";
				
				/* 메일~//~이름~//~010~//~1234~//~5678~//~411-224~//~경기도 고양시 일산서구~//~905동2001호 */
				str += mb.Email + param;
				str += mb.Name + param;
				string[] spliter1 = mb.Phone.Split('-');
				str += spliter1[0] + param + spliter1[1] + param + spliter1[2] + param;
				
				string[] spliter2 = mb.Address.Split('|');
				str += spliter2[0].Substring(0,3) + param + spliter2[0].Substring(4,3) + param + spliter2[1] + param + spliter2[2];
				ClientScript.RegisterHiddenField("hdnValues", str);	
				btnSave.Visible = false;
				btnModify.Visible = true;
			}
			
			if(!IsPostBack)
			{
			
			}
		}

		protected void btnSave_Clicked(object sender, EventArgs e)
		{
			mb.Email = Request.Form.Get("txtEmail").Trim();
			mb.Name = Request.Form.Get("txtName").Trim();
			mb.Phone = Request.Form.Get("txtPhone1").Trim() + "-" + Request.Form.Get("txtPhone2").Trim() + "-" + Request.Form.Get("txtPhone3").Trim();
			mb.Address = Request.Form.Get("txtPost1") + "-" + Request.Form.Get("txtPost2") + "|" + Request.Form.Get("txtAddress1") + "|" + Request.Form.Get("txtAddress2").Trim();
			
			dbAccess da = new dbAccess();
		
			da.RegisterUser(mb, Request.Form.Get("txtPswd").ToString());
			
			string key = Util.generateKey(20);
			da.AddTempUserRegisterKey(mb.Email, key);
			Mail.SendAuthenticationMail(mb.Email, mb.Name, key);
			Response.Redirect("RegisterComplete.aspx");
 		}
 		
 		protected void btnModify_Clicked(object sender, EventArgs e)
 		{
 			mb.Name = Request.Form.Get("txtName").Trim();
			mb.Phone = Request.Form.Get("txtPhone1").Trim() + "-" + Request.Form.Get("txtPhone2").Trim() + "-" + Request.Form.Get("txtPhone3").Trim();
			mb.Address = Request.Form.Get("txtPost1") + "-" + Request.Form.Get("txtPost2") + "|" + Request.Form.Get("txtAddress1") + "|" + Request.Form.Get("txtAddress2").Trim();

			dbAccess da = new dbAccess();
			
			da.ModifyUserInfo(mb);
			mb.FlushLoginTicket();
			Response.Redirect(Request.Url.ToString());
 		}
	}

}
